import cx_Oracle as cxo
import datetime as time

def io_table(fname,tname):
    conn=cxo.connect('scott/tiger@192.168.0.117/ORCL')
    c=conn.cursor()
    c.execute("truncate table EMP1")
    rs=c.execute("select column_name,data_type,data_precision,data_scale from user_tab_cols where table_name='EMP1'")
    meta=rs.fetchall()
    
    data=[]
    t_data=[]

    with open(fname) as f:
        for line in f:
            data.append(line.strip().replace('"','').split(','))
        
        
    for r_d in data:
        for i in range(len(meta)):
            if(meta[i][1]=='VARCHAR2'):
                continue
            if r_d[i]=='':
                r_d[i]=None
            else:
                if(meta[i][1]=='DATE'):
                    r_d[i]=time.datetime.strptime(r_d[i],'%Y/%m/%d')
                if(meta[i][1]=='NUMBER'):
                    if meta[i][3]==0:
                        r_d[i]=int(r_d[i])
                    if meta[i][3]==2:
                        r_d[i]=float(r_d[i])
        t_data.append(tuple(r_d))
       
    sql='insert into '+tname+' values('
    for i in range(1,len(meta)+1):
        sql+=':'+str(i)+','
    print(sql)
    print(data)
    print(t_data)
    sql=sql[:-1]+')'
    c.executemany(sql,t_data)
    conn.commit()

    c.close()
    conn.close
io_table('D:\\emp1.csv','EMP1')    
